<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-7-21
 * Func   追号记录
 */
defined('IN_YuLin') || exit('NO PERMIT!');

UserLogin(U($m.'/'.$c.'/'.$a));

$table = Table('lottery_order_track');
$oltable = Table('lottery_order_list');
$ltable = Table('lottery_period');
$ptable = Table('lottery_play');

if($a == 'cancel')	//	撤单
{
	EXIT;
	$cid = intval($_GET['gameid']);
	//!$cid && AjaxReturn(1,'彩票种类选择有误');
	
	$ids = NoBadStr($_GET['ids']);
	!$ids && AjaxReturn(1,'请选择要撤单的订单');
	
	$ids = explode(',',$ids);
	foreach($ids as $val)
	{
		$val = intval($val);
		if($val < 1)
		{
			continue;
		}
		
		$order = $db->getrow('SELECT l.money,l.status,p.status AS pstatus FROM '.$oltable.' l LEFT JOIN '.$ltable.' p ON l.iid = p.id WHERE l.id = '.$val.' AND l.uid = '.$_uid);
		!$order && AjaxReturn(1,'撤单的订单不存在！');
		($order['pstatus'] == 2) && AjaxReturn(1,'该订单已经开奖中，不可撤销');
		($order['pstatus'] == 3) && AjaxReturn(1,'该订单已经开奖，不可撤销');
		($order['status'] != 0) && AjaxReturn(1,'该订单不可撤销');
		
		if($order['money'] > 0)
		{
			$tmp = [];
			$tmp['uid']		=	$_uid;
			$tmp['tid']		=	'order_cancel';
			$tmp['lkey']	=	'order_cancel_'.$_uid.'_'.$val;
			$tmp['money']	=	$order['money'];
			$tmp['balance']	=	$order['money'];
			$tmp['notes']	=	'撤销订单：'.$val;
			$res = MoneyLog($tmp);
			!$res && AjaxReturn(1,'撤销订单失败！');
		}
		
		$db->exec('UPDATE '.$oltable.' SET status = 2 WHERE id = '.$val);
	}
	
	AjaxReturn(0,'撤单成功！');
}
else if($a == 'view')	//	详情
{
	$id = intval($_GET['id']);
	!$id && ShowMsg('查看的追号不存在',-1);
	
	$data = $db->getrow('SELECT t.* FROM '.$table.' t WHERE t.uid = '.$_uid.' AND t.id = '.$id.' AND t.status != 0');
	!$data && ShowMsg('查看的追号不存在',-1);
	
	//	玩法
	$t2 = $db->getrow('SELECT name FROM '.$ptable.' WHERE playid = '.$data['pid'].' AND cid = '.$data['cid']);
	$data['name'] = $t2['name'];
	
	$data['gname'] = $lottery[$data['cid']]['title'];
	
	//	投注详情
	$list = $db->getall('SELECT * FROM '.$oltable.' WHERE tid = '.$data['id'].' AND status NOT IN (3,4)');
	//	0正常单1已派奖2已撤单3已删除4下单失败99处理中
	$statuss = ['未开奖','已派奖','已撤单','已删除','下单失败',99=>'开奖中'];
	
	$tpl->display('member/ordertrack_view');
	exit;
}

$data  = [];
$where = ' FROM '.$table.' t WHERE t.uid = '.$_uid.' AND t.status != 0';

$cid = intval($_GET['cid']);
$cid && $where .= ' AND t.cid = '.$cid;

//	玩法
$pid = intval($_GET['pid']);
if($cid && $pid)
{
	$tmp = $db->getrow('SELECT * FROM '.Table('lottery_play_json').' WHERE cid = '.$cid);
	$tmp = json_decode($tmp['content'],true);
	
	$cids = [];
	foreach($tmp as $val)
	{
		if($val['Id'] == $pid)
		{
			foreach($val['c'] as $val2)
			{
				foreach($val2['C'] as $val3)
				{
					if(is_array($val3['C']))
					{
						foreach($val3['C'] as $val4)
						{
							$cids[] = $val4['I'];
						}
					}
					else
					{
						$cids[] = $val3['I'];
					}
				}
			}
			break;
		}
	}
	
	$cids && $where .= ' AND t.pid IN ('.implode(',',$cids).')';
}

$dt = NoBadStr($_GET['dtrange']);
$dt = explode(' - ',$dt);
!$dt[0] && $dt[0] = date('Y-m-d',strtotime('-1 day'));
!$dt[1] && $dt[1] = date('Y-m-d');
$dtrange = implode(' - ',$dt);

$where .= ' AND t.dateline >= '.strtotime($dt[0]).' AND t.dateline < '.(strtotime($dt[1]) + 86400);

$page = max(1,intval($_GET['page']));
$size = max(20,intval($_GET['size']));

//	统计
$num = ['money'=>0,'okmoney'=>0,'money2'=>0,'okmoney2'=>0];

//	状态	0作废1进行中2已结束
$status = intval($_GET['status']);
if($status == 1)	//	未中奖
{
	$where .= ' AND t.prize = 0';
}
else if($status == 2)	//	已中奖
{
	$where .= ' AND t.prize > 0';
}

$tmp = $db->getrow('SELECT COUNT(1) AS p '.$where);
if($tmp['p'])
{
	$orderby = ' ORDER BY t.id DESC ';
	$limit = ' LIMIT '.($page - 1) * $size.','.$size;
	
	$data = $db->getall('SELECT t.* '.$where.$orderby.$limit);
	foreach($data as $key => $val)
	{
		//	玩法
		$t2 = $db->getrow('SELECT name FROM '.$ptable.' WHERE playid = '.$val['pid'].' AND cid = '.$val['cid']);
		$data[$key]['name'] = $t2['name'];
		
		$num['money'] += $val['money'];
		$num['okmoney'] += $val['okmoney'];
	}
	
	$url = U($m.'/'.$c,['dtrange'=>$dtrange,'cid'=>$cid,'pid'=>$pid,'status'=>$status]);
	$multi = Multi($url,$page,$tmp['p'],$size);
	
	$tmp = $db->getrow('SELECT SUM(money) AS money,SUM(okmoney) AS okmoney '.$where);
	$num['money2'] = round($tmp['money'],2);
	$num['okmoney2'] = round($tmp['okmoney'],2);
}

//	彩票种类
$cids = $lottery;

$head['title'] = '追号记录';

$tpl->display('member/ordertrack');